package com.wwwarehouse.contract.orders.place_order;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.AddressListBean;
import com.wwwarehouse.contract.bean.ConfirmOrderBean;
import com.wwwarehouse.contract.bean.ConfirmOrderParams;
import com.wwwarehouse.contract.contract.TheParentFragment;
import com.wwwarehouse.contract.contract_address.ChooseAddressViewPagerFragment;
import com.wwwarehouse.contract.contract_address.UpdateAddressFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.core.ContractEventCode;
import com.wwwarehouse.contract.event.AddAddressForConfirmEvent;
import com.wwwarehouse.contract.event.ChooseAddressEvent;
import com.wwwarehouse.contract.event.ContractEvent;
import com.wwwarehouse.contract.event.RefreshConfirmOrderEvent;

import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/19.
 * 确认订单
 */
public class ConfirmOrderFragment extends TheParentFragment implements View.OnClickListener {
	private ImageView mLogoIv;
	private TextView mNameTv;
	private TextView mGoodsKindTv;
	private TextView mGoodsNumTv;
	private TextView mPeriodPayTv;
	private TextView mOrderPayTv;
	private TextView mAlipayTv;
	private TextView mWechatTv;
	private TextView mOfflineTv;
	private TextView mBankTv;
	private LinearLayout mAddLayout;  //新增地址
	private TextView mAddTv;
	private LinearLayout mEditAddressLayout; //修改地址
	private TextView mShipTv;
	private TextView mLinkManTv;
	private TextView mPhoneTv;
	private TextView mEditTv;
	private TextView mTotalTv;
	private Button mConfirmBtn;
	private String demanId;
	private String addressUkid;
	private String mAddress;
	private String mPhone;
	private String mName;
	private String supplierBusinessId;
	private String cartItemUkid;
	private int mKindNum;
	private int mCounNum;

	@Override
	protected int getContentId() {
		mLoadingView.setVisibility(View.VISIBLE);
		return R.layout.contract_f_confirm_order_fragment;
	}


	@Override
	protected void init(View view) {
		EventBus.getDefault().register(this);
		mLogoIv = (ImageView) view.findViewById(R.id.logo_iv);
		mNameTv = (TextView) view.findViewById(R.id.name_tv);
		mGoodsKindTv = (TextView) view.findViewById(R.id.kind_tv);
		mGoodsNumTv = (TextView) view.findViewById(R.id.goods_num_tv);
		mPeriodPayTv = (TextView) view.findViewById(R.id.period_pay_tv);
		mOrderPayTv = (TextView) view.findViewById(R.id.order_pay_tv);
		mAlipayTv = (TextView) view.findViewById(R.id.alipay_tv);
		mWechatTv = (TextView) view.findViewById(R.id.wechat_tv);
		mOfflineTv = (TextView) view.findViewById(R.id.offline_tv);
		mAddLayout = (LinearLayout) view.findViewById(R.id.add_address_layout);
		mAddTv = (TextView) view.findViewById(R.id.add_address_tv);
		mEditAddressLayout = (LinearLayout) view.findViewById(R.id.edit_address_layout);
		mShipTv = (TextView) view.findViewById(R.id.shiping_tv);
		mLinkManTv = (TextView) view.findViewById(R.id.link_man_tv);
		mPhoneTv = (TextView) view.findViewById(R.id.phone_tv);
		mEditTv = (TextView) view.findViewById(R.id.edit_address_tv);
		mConfirmBtn = (Button) view.findViewById(R.id.btn_confirm);
		mTotalTv = (TextView) view.findViewById(R.id.total_money_tv);
		mBankTv = (TextView) view.findViewById(R.id.bank_tv);
		mConfirmBtn.setOnClickListener(this);
		mEditTv.setOnClickListener(this);
		mAddTv.setOnClickListener(this);
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ConfirmOrderFragment) {
			mActivity.setTitle("确认订单");
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getSerializable(ContractConstant.KEY_BUNDLE_CONFIRM_ORDER) != null) {
			ConfirmOrderParams params = (ConfirmOrderParams) getArguments().getSerializable(ContractConstant.KEY_BUNDLE_CONFIRM_ORDER);
			if (params != null && StringUtils.isNoneNullString(params.getLogo())) {
				BaseApplication.getApplicationInstance().displayImg(params.getLogo(), mLogoIv);
			}
			if (params != null && StringUtils.isNoneNullString(params.getName())) {
				mNameTv.setText(params.getName());
			}
			if (params != null ) {
				Map<String, Object> map = new HashMap<>();
				if (StringUtils.isNoneNullString(params.getDemandId())) {
					demanId = params.getDemandId();
					map.put("demandId", demanId);
				}
				if (StringUtils.isNoneNullString(params.getSupplierBusinessId())) {
					supplierBusinessId = params.getSupplierBusinessId();
					map.put("supplierBusinessId", supplierBusinessId);
				}
				if (StringUtils.isNoneNullString(params.getCartItemUkid())) {
					cartItemUkid = params.getCartItemUkid();
					map.put("cartItemUkid", cartItemUkid);
				}
				httpPost(ContractConstant.CONFIRM_ORDER_METHOD, map, 1, false, "");
			}
		}

	}

	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (what == 1) {
			ConfirmOrderBean confirmOrderBean = JSON.parseObject(commonClass.getData().toString(), ConfirmOrderBean.class);
			if (confirmOrderBean != null) {
					mGoodsKindTv.setText(StringUtils.getResourceStr(mActivity, R.string.goods_kind_num, confirmOrderBean.getSkuCount()));
					mGoodsNumTv.setText(StringUtils.getResourceStr(mActivity, R.string.goods_count, confirmOrderBean.getItemCount()));
				mKindNum = confirmOrderBean.getSkuCount();
				mCounNum = confirmOrderBean.getItemCount();
				if (StringUtils.isNoneNullString(confirmOrderBean.getPeriodPay())) {
					mPeriodPayTv.setText(StringUtils.getResourceStr(mActivity, R.string.place_order_money_string, confirmOrderBean.getPeriodPay()));
				}
				if (StringUtils.isNoneNullString(confirmOrderBean.getOrderPay())) {
					mOrderPayTv.setText(StringUtils.getResourceStr(mActivity, R.string.place_order_money_string, confirmOrderBean.getOrderPay()));
				}
				if (StringUtils.isNoneNullString(confirmOrderBean.getAliPay())) {
					if (Double.valueOf(confirmOrderBean.getAliPay()) == 0) {
						mAlipayTv.setVisibility(View.GONE);
					} else {
						mAlipayTv.setText(StringUtils.getResourceStr(mActivity, R.string.alipay_num, confirmOrderBean.getAliPay()));
					}
				}
				if (StringUtils.isNoneNullString(confirmOrderBean.getBank())) {
					if (Double.valueOf(confirmOrderBean.getBank()) == 0) {
						mBankTv.setVisibility(View.GONE);
					} else {
						mBankTv.setText(StringUtils.getResourceStr(mActivity, R.string.bank_pay_title, confirmOrderBean.getBank()));
					}
				}
				if (StringUtils.isNoneNullString(confirmOrderBean.getWechatPay())) {
					if (Double.valueOf(confirmOrderBean.getWechatPay()) == 0) {
						mWechatTv.setVisibility(View.GONE);
					} else {
						mWechatTv.setText(StringUtils.getResourceStr(mActivity, R.string.wechat_pay_num, confirmOrderBean.getWechatPay()));
					}
				}
				if (StringUtils.isNoneNullString(confirmOrderBean.getOffline())) {
					if (Double.valueOf(confirmOrderBean.getOffline()) == 0) {
						mOfflineTv.setVisibility(View.GONE);
					} else {
						mOfflineTv.setText(StringUtils.getResourceStr(mActivity, R.string.offline_pay_num, confirmOrderBean.getOffline()));
					}
				}
				if (StringUtils.isNoneNullString(confirmOrderBean.getTotalPrice())) {
					mTotalTv.setText(StringUtils.getResourceStr(mActivity, R.string.place_order_money_string, confirmOrderBean.getTotalPrice()));
				}
				if (StringUtils.isNoneNullString(confirmOrderBean.getAddress())) {
					mConfirmBtn.setEnabled(true);
					mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_normal_shape));
					mEditAddressLayout.setVisibility(View.VISIBLE);
					mAddLayout.setVisibility(View.GONE);
					mShipTv.setText(StringUtils.getResourceStr(mActivity, R.string.shipping_address, confirmOrderBean.getAddress()));
					mAddress = confirmOrderBean.getAddress();
					if (StringUtils.isNoneNullString(confirmOrderBean.getReceiveName())) {
						mLinkManTv.setText(StringUtils.getResourceStr(mActivity, R.string.link_man_tip, confirmOrderBean.getReceiveName()));
						mName = confirmOrderBean.getReceiveName();
					}
					if (StringUtils.isNoneNullString(confirmOrderBean.getMobilePhone())) {
						mPhoneTv.setText(confirmOrderBean.getMobilePhone());
						mPhone = confirmOrderBean.getMobilePhone();
					}
						addressUkid = String.valueOf(confirmOrderBean.getAddressCommonUkid());
				} else {
					mConfirmBtn.setEnabled(false);
					mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_noclick_shape));
					mEditAddressLayout.setVisibility(View.GONE);
					mAddLayout.setVisibility(View.VISIBLE);
				}


			}
		} else if (what == 2) {
			if (commonClass.getCode().equals("0")) {
				popFragment();
				Bundle bundle = new Bundle();
				bundle.putInt("kind", mKindNum);
				bundle.putInt("count", mCounNum);
				bundle.putString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID , demanId);
				PlaceOrderSuccessFragment placeOrderSuccessFragment = new PlaceOrderSuccessFragment();
				placeOrderSuccessFragment.setArguments(bundle);
				pushFragment(placeOrderSuccessFragment);
			} else {
				if (StringUtils.isNoneNullString(commonClass.getMsg())) {
					toast(commonClass.getMsg());
				}
			}
		}


	}

	@Override
	public void onReLoad(int code) {

	}

	@Override
	public void onClick(View v) {
		if (v == mEditTv) {
			//更改地址地址
			Bundle bundle = new Bundle();
			bundle.putString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID, demanId);
			bundle.putString(ContractConstant.KEY_ADDRESS_ADDRESSUKID ,addressUkid );
			ChooseAddressViewPagerFragment chooseAddressViewPagerFragment = new ChooseAddressViewPagerFragment();
			chooseAddressViewPagerFragment.setArguments(bundle);
			pushFragment(chooseAddressViewPagerFragment);
		} else if (v.getId() == R.id.add_address_tv) {
			//新增地址
			Bundle bundle = new Bundle();
			bundle.putString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID, demanId);
			bundle.putString(ContractConstant.KEY_ADDRESS_ADDRESSUKID, addressUkid);
			bundle.putString(ContractConstant.KEY_CONTRACT_ADD_ADDRESS, "add");
			UpdateAddressFragment addressFragment = new UpdateAddressFragment();
			addressFragment.setArguments(bundle);
			pushFragment(addressFragment);
		} else if (v == mConfirmBtn) {
			//确认按钮
			Map<String, Object> map = new HashMap<>();
			map.put("addressCommonUkid", addressUkid);
			map.put("demandId", demanId);
			map.put("cartItemUkid", cartItemUkid);
			map.put("supplierBusinessId", supplierBusinessId);
			httpPost(ContractConstant.CONFIRM_ORDER_SUBMIT_METHOD , map , 2 , true , "正在保存...");
		}
	}

	public void onEventMainThread(ChooseAddressEvent event) {
		if (peekFragment() instanceof ConfirmOrderFragment) {
			AddressListBean.ListBean bean = event.getAddress();
			if (bean != null) {
				mAddLayout.setVisibility(View.GONE);
				mEditAddressLayout.setVisibility(View.VISIBLE);
				if (StringUtils.isNoneNullString(bean.getAddress())) {
					mShipTv.setText(bean.getAddress());
					mAddress = bean.getAddress();

				}
				if (StringUtils.isNoneNullString(bean.getReceiveName())) {
					mLinkManTv.setText(StringUtils.getResourceStr(mActivity, R.string.link_man_tip, bean.getReceiveName()));
					mName = bean.getReceiveName();

				}
				if (StringUtils.isNoneNullString(bean.getMobilePhone())) {
					mPhoneTv.setText(bean.getMobilePhone());
					mPhone = bean.getMobilePhone();

				}
				if (StringUtils.isNoneNullString(bean.getAddressCommonUkid())) {
					addressUkid = bean.getAddressCommonUkid();
				}
			}
		}

	}
	public void onEventMainThread(AddAddressForConfirmEvent event) {
		if (peekFragment() instanceof ConfirmOrderFragment) {
			if (event != null) {
				mAddLayout.setVisibility(View.GONE);
				mConfirmBtn.setEnabled(true);
				mConfirmBtn.setBackground(mActivity.getResources().getDrawable(R.drawable.common_btn1_normal_shape));
				mEditAddressLayout.setVisibility(View.VISIBLE);
				if (StringUtils.isNoneNullString(event.getAddressId())) {
					addressUkid = event.getAddressId();
				}
				if (StringUtils.isNoneNullString(event.getAddress())) {
					mShipTv.setText(event.getAddress());
					mAddress = event.getAddress();
				}
				if (StringUtils.isNoneNullString(event.getName())) {
					mLinkManTv.setText(StringUtils.getResourceStr(mActivity, R.string.link_man_tip, event.getName()));
					mName = event.getName();
				}
				if (StringUtils.isNoneNullString(event.getPhone())) {
					mPhoneTv.setText(event.getPhone());
					mPhone = event.getPhone();
				}
			}
		}
	}

	@Override
	protected void receiveEvent(ContractEvent contractEvent) {
		if (peekFragment() instanceof ConfirmOrderFragment) {
			if (contractEvent.getCode() == ContractEventCode.CODE_REFRESH_CONFIRM_ORDER) {
				RefreshConfirmOrderEvent event = (RefreshConfirmOrderEvent) contractEvent.getData();
				if (StringUtils.isNoneNullString(event.getMsg())) {
					requestDatas();
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}
